import logging
import os
import requests

from base.jsonpath_utils import get_jsonpath_value
from base.os_utils import get_env_param

is_execute_status = 0
sn = None
image = None

def get_sn_and_verify_code():
    logging.info("========================开始执行动态依赖中的获取验证码接口===================================")
    global is_execute_status
    global sn
    global image

    if is_execute_status == 0:
        logging.info(f"当前is_execute_status状态是:{is_execute_status},0说明可以执行调用获取验证码接口,开始执行")
        request_data = {
            "url": get_env_param('base_url') + "/api/cloud-api/auth/captcha",
            "method": "get",
            "headers": None,
            "params": None,
            "data": None,
            "json": None,
            "files": None
        }
        try:
            response_data_obj = requests.request(**request_data)
            sn = get_jsonpath_value(response_data_obj.json(), "$..sn")
            image = get_jsonpath_value(response_data_obj.json(), "$..image")
        except:
            logging.error("执行获取验证码接口出现异常了")
        else:
            logging.info("执行获取验证码接口成功了")
            logging.info(f"获取到sn的值为:{sn}")
            logging.info(f"获取到image的值为:{image}")
            logging.info("========================动态依赖中的获取验证码接口执行完毕===================================")
            is_execute_status = 1
            return sn, image
    else:
        logging.info(f"当前is_execute_status状态是:{is_execute_status},1说明不可以执行调用获取验证码接口,不执行")
        is_execute_status = 0
        return sn, image
